package cc.spider.demo.web;

import cc.spider.web.parser.JsoupParser;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class ParserMain {
    public static void main(String[] args) {
        ParserMain main = new ParserMain();
        String baseUri = "http://www.123.com/";
        String html = main.getHtml();
        JsoupParser parser = new JsoupParser(html, baseUri);
        System.out.println(parser.queryAll("#table tr td:eq(1)").text());
    }

    private String getHtml() {
        try {
            return this.getResourcesFileContent("parser.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    private String getResourcesFileContent(String path) throws IOException {
        InputStream ips = this.getClass().getClassLoader().getResourceAsStream(path);
        byte[] buffer = new byte[1024];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int eof;
        while ((eof = ips.read(buffer)) != -1) {
            baos.write(buffer, 0, eof);
        }
        baos.close();
        ips.close();
        return baos.toString(StandardCharsets.UTF_8.toString());
    }
}
